<?php
if(isset($_REQUEST['frm_aksi'])){
	if($_REQUEST['frm_aksi']=="input"){
		require_once('../class/class.cuti.php');
		require_once '../class/class.pegawai.php';
		require_once '../class/class.refcuti.php';

		$str=$_REQUEST['id_cuti'];

		$exp=explode(",", $str);

		$id=$exp[0];
		$cuti="";
		$pegawai="";
		$ref="";

		$cutiDao=new CutiDao();
		$cuti=$cutiDao->getCuti($id);
		$msg=false;
		

		if($cuti!=false){
			$msg=true;
			$pegawaiDao=new PegawaiDao();
			$pegawai=$pegawaiDao->getPegawai($cuti['nip']);
			$refDao=new RefCutiDao();
			$ref=$refDao->getCuti($cuti['idJnsCuti']);

		}

		$data=array(
				'msg'=> $msg,
				'cuti' => $cuti,
				'pegawai' => $pegawai,
				'ref' => $ref,
				'input' =>true,
				'ubah' => false,
				);

		echo json_encode($data);
	}

	if($_REQUEST['frm_aksi']=="ubah"){
		require_once('../class/class.cuti.php');
		require_once '../class/class.pegawai.php';
		require_once '../class/class.refcuti.php';
		require_once('../class/class.detailkartu.php');

		$str=$_REQUEST['id_cuti'];

		$detailDao=new DetailKartuDao();



		$exp=explode(",", $str);

		$id=$exp[0];
		$cuti="";
		$pegawai="";
		$ref="";

		$kartu=$detailDao->getDetail($id);

		$cutiDao=new CutiDao();
		$cuti=$cutiDao->getCuti($id);
		$msg=false;
		

		if($cuti!=false){
			$msg=true;
			$pegawaiDao=new PegawaiDao();
			$pegawai=$pegawaiDao->getPegawai($cuti['nip']);
			$refDao=new RefCutiDao();
			$ref=$refDao->getCuti($cuti['idJnsCuti']);

		}

		$data=array(
				'msg'=> $msg,
				'cuti' => $cuti,
				'pegawai' => $pegawai,
				'ref' => $ref,
				'input' =>false,
				'kartu' => $kartu,
				'ubah' => true,
				);

		echo json_encode($data);
	}

	if($_REQUEST['frm_aksi']=="save"){
		require_once('../class/class.cuti.php');
		require_once('../class/class.kartu.php');
		require_once('../class/class.batal.php');
		require_once('../class/class.detailkartu.php');

		$str=$_REQUEST['id_cuti'];

		$exp=explode(",", $str);
		

		$id=$exp[0];

		$cutiDao=new CutiDao();
		$cuti=$cutiDao->getCuti($id);
		$msg=false;
		$sukses="Berhasil Dikartukan";
		$error="Pengartuan Cuti Gagal";

		$nip=$cuti['nip'];

		$tgl=date('Y-m-d', strtotime($_REQUEST['tgl_surat']));
		$no=$_REQUEST['nomor_surat'];

		$kartuDao=new KartuDao();

		$kartu=$kartuDao->getKartuByNip($nip);
		$idKartu= $kartu['id'];

		$batalDao=new BatalDao();

		if(count($exp)==3){
			$idBatal=$exp[2];
			$batal=$batalDao->getBatalById($idBatal);
			$jmlCutiBatal=$batal['jmlHariBatal'];
		}else{
			$idBatal='';
			$jmlCutiBatal=0;
		}
		$jmlHari=$cuti['jmlHari'];

		$detailkartu= new DetailKartu();
		$detailkartu->idKartu=$idKartu;
		$detailkartu->idCuti=$id;
		$detailkartu->idBatal=$idBatal;
		$detailkartu->nomorSurat=$no;
		$detailkartu->tglSurat=$tgl;
		$detailkartu->jmlCuti=$jmlHari;
		$detailkartu->jmlCutiBatal=$jmlCutiBatal;

		$detailDao=new DetailKartuDao();

		$create=$detailDao->createKartu($detailkartu);

		if($create){
			$msg=true;
			$cutiDao->kartukanCuti($id);
		}

		$data=array(
			'msg' => $msg,
			'eror' => $error,
			'sukses' => $sukses,
			'input' => false,
			'ubah' => false,
			);

		echo json_encode($data);

	}
	if($_REQUEST['frm_aksi']=="simpan_edit"){
		require_once('../class/class.cuti.php');
		require_once('../class/class.kartu.php');
		require_once('../class/class.detailkartu.php');

		
		$msg=false;
		$sukses="Berhasil Ubah";
		$error="Gagal Ubah";

		$tgl=date('Y-m-d', strtotime($_REQUEST['tgl_surat']));
		$no=$_REQUEST['nomor_surat'];

		$id= $_REQUEST['id_aksi'];

		$detailkartu= new DetailKartu();
		$detailkartu->id=$id;
		$detailkartu->nomorSurat=$no;
		$detailkartu->tglSurat=$tgl;

		$detailDao=new DetailKartuDao();

		$create=$detailDao->updateKartu($detailkartu);

		if($create){
			$msg=true;
		}

		$data=array(
			'msg' => $msg,
			'eror' => $error,
			'sukses' => $sukses,
			'input' => false,
			'ubah' => false,
			);

		echo json_encode($data);

	}
}
?>